package nl.cwi.arne.lts.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import nl.cwi.arne.lts.LTS;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;

public class LTSResource extends ResourceImpl {
	
	/**
	 * File extension for LTS files.
	 */
	public static final String FILE_EXTENSION = "aut";
	
	/**
	 * Default constructor.
	 */
	public LTSResource() {
		super();
	}
	
	/**
	 * Convenience constructor.
	 * @param uri URI of the resource.
	 */
	public LTSResource(URI uri) {
		super(uri);
	}
	
	public LTS getLTS() {
		for (EObject item : getContents()) {
			if (item instanceof LTS) return (LTS) item;
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.emf.ecore.resource.impl.ResourceImpl#doSave(java.io.OutputStream, java.util.Map)
	 */
	@Override
	protected void doSave(OutputStream out, Map<?, ?> options) throws IOException {
		LTSPrinter printer = new LTSPrinter();
		printer.print(getLTS(), out);
		out.flush();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.emf.ecore.resource.impl.ResourceImpl#doLoad(java.io.InputStream, java.util.Map)
	 */
	@Override
	protected void doLoad(InputStream in, Map<?, ?> options) throws IOException {
		LTSParser parser = new LTSParser();
		getContents().add(parser.parse(in));
	}
	
}
